
package kaizone.haining.chaobohui.widget;

import kaizone.haining.chaobohui.R;
import kaizone.haining.chaobohui.activity.CBHActivity;
import kaizone.haining.chaobohui.fragment.CBHContentFragment;
import kaizone.haining.chaobohui.fragment.DetailUsersFragment;
import kaizone.haining.chaobohui.json.UserRegister;
import android.content.Context;
import android.content.res.Resources;
import android.support.v4.app.Fragment;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ForegroundColorSpan;
import android.util.AttributeSet;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class UsersDetail extends LinearLayout implements OnClickListener {

    private TextView mNameTextView;

    private TextView mFollowTextView;

    private TextView mJoinTextView;

    private TextView mFavoritesTextView;

    private TextView mNotificeTextView;

    private ImageView mPhotosImageView;

    private ImageView mMessageImageView;

    public UsersDetail(Context context) {
        super(context);
    }

    public UsersDetail(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {

    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        mNameTextView = (TextView) findViewById(R.id.tv01);
        mFollowTextView = (TextView) findViewById(R.id.tv02);
        mJoinTextView = (TextView) findViewById(R.id.tv03);
        mFavoritesTextView = (TextView) findViewById(R.id.tv04);
        mNotificeTextView = (TextView) findViewById(R.id.tv05);
        mPhotosImageView = (ImageView) findViewById(R.id.img01);
        mMessageImageView = (ImageView) findViewById(R.id.img02);
        initWidget();
    }

    private void initWidget() {
        mFollowTextView.setOnClickListener(this);
        mJoinTextView.setOnClickListener(this);
        mFavoritesTextView.setOnClickListener(this);
        mNotificeTextView.setOnClickListener(this);
    }

    public void setData(Object data) {
        if (data == null)
            return;
        if (data instanceof UserRegister) {
            UserRegister user = (UserRegister) data;
            final String userName = "(" + user.userName.replace("user", "uid:") + ")";
            mNameTextView.setText(userName);

            final SpannableString ssFolowActCount = getSpannable(user.followActivityCount);
            final SpannableString ssCheckinActCount = getSpannable(user.checkinActivityCount);
            final SpannableString ssFavoriteSubCount = getSpannable(user.favoriteSubjectCount);

            mFollowTextView.setText("我关注的活动\n");
            mFollowTextView.append(ssFolowActCount);
            // mJoinTextView.append(user.checkinActivityCount);
            mJoinTextView.setText("我参加的活动\n");
            mJoinTextView.append(ssCheckinActCount);
            mFavoritesTextView.setText("我收藏的专题\n");
            mFavoritesTextView.append(ssFavoriteSubCount);
        }

    }

    @Override
    public void onClick(View v) {
        Resources res = getContext().getResources();
        Fragment f = null;
        String str = null;
        if (v == mFollowTextView) {
            f = DetailUsersFragment.newInstance(DetailUsersFragment.MY_ACT_FOLLOWS);
            str = res.getString(R.string.wgzdhd);
        } else if (v == mJoinTextView) {
            f = DetailUsersFragment.newInstance(DetailUsersFragment.MY_ACT_CHECKINS);
            str = res.getString(R.string.wcjdhd);
        } else if (v == mFavoritesTextView) {
            f = DetailUsersFragment.newInstance(DetailUsersFragment.MY_SUBJ_FOLLOWS);
            str = res.getString(R.string.wscdzt);
        } else if (v == mNotificeTextView) {
            f = DetailUsersFragment.newInstance(DetailUsersFragment.FLAG_NOTIFICE);
            str = res.getString(R.string.wzjdpl);
        }
        switchContent(str, f);
    }

    public void switchContent(String str, Fragment f) {
        if (f == null)
            return;

        if (getContext() instanceof CBHActivity) {
            CBHActivity cbhActivity = (CBHActivity) getContext();
            Fragment fragment = cbhActivity.getContent();
            if (fragment instanceof CBHContentFragment) {
                CBHContentFragment content = (CBHContentFragment) fragment;
                content.setTitle(str);
                content.switchContent(f);
                content.setTitleLeftImageResource(R.drawable.btn_backwhite);
                // content.addContentToBackStack(f);
            }
        }
    }

    public SpannableString getSpannable(String str) {
        if (TextUtils.isEmpty(str))
            return null;
        SpannableString msp = new SpannableString(str);
        msp.setSpan(new AbsoluteSizeSpan(16, true), 0, str.length(),
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        int color = getResources().getColor(R.color.black);
        msp.setSpan(new ForegroundColorSpan(color), 0, str.length(),
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        return msp;
    }

}
